import time

def bubble_sort(nums):
    n = len(nums) - 1
    is_sorted = all(nums[i]<=nums[i+1] for i in range(n))
    is_reverse_sorted = all(nums[i]>=nums[i+1] for i in range(n))

    if is_sorted: return
    if is_reverse_sorted: return nums.reverse()
    
    for i in range(n):
        swapped = False
        for j in range(n - i):
            if nums[j] > nums[j + 1]:
                nums[j], nums[j + 1] = nums[j + 1], nums[j]
                swapped = True
        if not swapped:
            break

def read_to_list(filename):
    with open(filename, 'r') as file:
        return [int(line) for line in file]

filenames = ["m1_sorted.txt", "m2_reverse.txt", "m3_shuffle.txt"]

for filename in filenames:
    m = read_to_list(filename)
    # print(m[:30])
    start_time = time.perf_counter()
    bubble_sort(m)
    end_time = time.perf_counter()
    print(filename.split('.')[0], round((end_time - start_time) * 1000), "milliseconds")
    # print(m[:30])